package im.yisme.view.ui.animation;

import im.yisme.R;
import im.yisme.view.ui.RecordView;
import im.yisme.view.ui.animation.pojo.HaloPOJO;
import im.yisme.view.ui.util.UIparams;

import java.util.ArrayList;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.Log;
/**
 * Halo动画逻辑
 * @author Max
 *
 */
public class Halo extends Thread{

	private HaloPOJO halo;
	private static final float MAX = 50;
	private static final float EXTEND = 40;
	private static final int ANIMATION_UPDATE_INTERVAL = 4;
	private static final int PAUSE_POLLING_INTERVAL = 500;
	private float count;
	private ArrayList<HaloPOJO> list;
	private ArrayList<Halo> threadList;
    private boolean pause;
	
    private Bitmap torus;
    private PointF center;
    private int r;
    
	public Halo(PointF center, int r ,ArrayList<HaloPOJO> haloArrayList,ArrayList<Halo> threadList,Resources res) {
		
		torus = BitmapFactory.decodeResource(res, R.drawable.game_halo);
		this.halo = new HaloPOJO(new PointF(center.x-r,center.y-r), torus, 255);
		count = 0;
		this.center = center;
		this.r = r;
		list = haloArrayList;
		this.threadList= threadList;
		list.add(this.halo);
		this.threadList.add(this);
		pause=false;
	}

	public void run() {

	  try {
		  while (count <= MAX) {

			while(pause) Thread.sleep(PAUSE_POLLING_INTERVAL);
				
			float extend= (count / MAX) * EXTEND;
			
			float scale = (extend+r)/r;		
			Bitmap torusTemp = zoomIn(torus, scale);
			halo.setHalo(torusTemp);
			
			PointF point = new PointF(center.x - r - extend,
					                  center.y - r - extend );
			halo.setHaloPointF(point);
					
			int tempAlpha = (int) (255-255 * (count / MAX));
			halo.setAlpha(tempAlpha);
			
			
			Thread.sleep(ANIMATION_UPDATE_INTERVAL);
			
			
			count+=2;
			
		 } 
		 } catch (Exception e) {
			// TODO: handle exception
		 }
	   	
		list.remove(halo);
		threadList.remove(this);
	}
	public void pauseRunning(){
		pause=true;
	}
	public void resumeRunning(){
		pause=false;
	}
	private Bitmap zoomIn(Bitmap input,float scale){
		
		Matrix matrix = new Matrix();
	
        matrix.postScale(scale, scale);
        
        Bitmap output = Bitmap.createBitmap(input, 0, 0, input.getWidth(), input.getHeight(), matrix, false);
        
        
       
        return output;
	}
}
